package de.carcophan.jimdb.service;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.jboss.logging.Logger;

import de.carcophan.jimdb.gwt.server.util.PasswordUtil;
import de.carcophan.jimdb.gwt.shared.entites.Movie;
import de.carcophan.jimdb.gwt.shared.entites.Tag;
import de.carcophan.jimdb.gwt.shared.entites.User;
import de.carcophan.jimdb.gwt.shared.entites.UserMovies;

public class QueryController {

	private static Logger logger = Logger.getLogger(QueryController.class);

	@Inject
	private PasswordUtil passwordUtil;

	@PersistenceContext
	private EntityManager em;

	public List<Movie> findMoviesByTitleSubstring(String titleSubstring,
			int maxResult) {
		logger.info("QueryController.findMoviesByTitleSubstring() "
				+ titleSubstring);
		String queryString = "SELECT m FROM Movie m where m.title LIKE :pattern";
		String pattern = "%" + titleSubstring + "%";
		TypedQuery<Movie> query = em.createQuery(queryString, Movie.class);
		query.setParameter("pattern", pattern);
		query.setMaxResults(maxResult);
		List<Movie> result = query.getResultList();
		return result;
	}

	public Movie findMovieByJimdbId(String imdbId) {
		logger.info("QueryController.findMovieByJimdbId() " + imdbId);
		Movie result = null;
		try {
			String queryString = "SELECT m FROM Movie m where m.imdbId = :imdbId";
			TypedQuery<Movie> query = em.createQuery(queryString, Movie.class);
			query.setParameter("imdbId", imdbId);
			result = query.getSingleResult();
		} catch (Exception e) {
			logger.info(e.getMessage());
			result = null;
		}
		return result;
	}

	public User findUser(String login, String password) {
		logger.debug("QueryController.findUser() " + login);
		User user;
		try {
			String hash = passwordUtil.getHash(password);
			TypedQuery<User> query = em
					.createQuery(
							"SELECT u FROM User u where u.login=:login AND u.password=:password",
							User.class);
			query.setParameter("login", login);
			query.setParameter("password", hash);
			user = query.getSingleResult();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return user;
	}

	public List<Tag> findAllTags() {
		TypedQuery<Tag> tagsQuery = em.createQuery(
				"SELECT t FROM Tag t ORDER BY t.name ASC", Tag.class);
		List<Tag> tags = tagsQuery.getResultList();
		return tags;
	}

	public List<UserMovies> findUserMovie(String imdbId, int id) {
		String queryString = "SELECT um FROM UserMovies um WHERE um.user.id = :id AND um.movie.imdbId = :imdbId";
		TypedQuery<UserMovies> query = em.createQuery(queryString,
				UserMovies.class);
		query.setParameter("id", id);
		query.setParameter("imdbId", imdbId);
		List<UserMovies> result = query.getResultList();
		return result;
	}

	public UserMovies findSingleUserMovie(String imdbId, int userId) {
		String queryString = "SELECT um FROM UserMovies um WHERE um.user.id = :id AND um.movie.imdbId = :imdbId";
		TypedQuery<UserMovies> query = em.createQuery(queryString,
				UserMovies.class);
		query.setParameter("id", userId);
		query.setParameter("imdbId", imdbId);
		UserMovies result = null;
		try {
			result = query.getSingleResult();
		} catch (NoResultException e) {
			logger.info(e.getMessage());
		}
		return result;
	}

	public Tag findTagByName(String selectedTag) {
		String queryString = "SELECT t FROM Tag t where t.name = :name";
		TypedQuery<Tag> query = em.createQuery(queryString, Tag.class);
		query.setParameter("name", selectedTag);
		Tag result = null;
		try {
			result = query.getSingleResult();
		} catch (NoResultException e) {
			logger.info(e.getMessage());
		}
		return result;
	}

	public List<UserMovies> loadMovies() {
		String queryString = "SELECT u FROM UserMovies u";
		TypedQuery<UserMovies> query = em.createQuery(queryString, UserMovies.class);
		return query.getResultList();
	}

}
